package com.example.meitao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.meitao.R;
import com.example.meitao.bean.ChunTu;
import com.example.meitao.config.NetConfig;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

/**
 * Created by 星星 on 2016/8/13.
 */
public class ChunTuAdapter extends RecyclerView.Adapter<ChunTuAdapter.ViewHolder>{
    private List<ChunTu.Itemsbean> data;
    private Context context;
    private ImageOptions options;
    public ChunTuAdapter(List<ChunTu.Itemsbean> data, Context context) {
        this.data = data;
        this.context = context;
        options = new ImageOptions.Builder().setCircular(true).build();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_tu,null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ChunTu.Itemsbean itemsbean = data.get(position);
        ChunTu.Itemsbean.Userbean user = itemsbean.getUser();
        if(user!=null&&!TextUtils.isEmpty(user.getIcon())) {
            String path = String.format(NetConfig.URL_USER_ICON, user.getId() / 10000, user.getId(), user.getIcon());
            x.image().bind(holder.imageView,path,options);
        }
        if(user!=null&&!TextUtils.isEmpty(user.getLogin())) {
            holder.tvName.setText(user.getLogin());
        }
        ChunTu.Itemsbean.HotCommentbean comment = itemsbean.getHot_comment();
        if(comment!=null&&!TextUtils.isEmpty(comment.getContent())) {
            holder.tvContent.setText(comment.getContent());
        }
        String image = String.format(NetConfig.URL_USER_IMAGE,itemsbean.getId()/10000,itemsbean.getId(),itemsbean.getImage());
        x.image().bind(holder.imageView2,image);
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tvName,tvContent;
        private ImageView imageView,imageView2;
        public ViewHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_tu_item_name);
            tvContent = (TextView) itemView.findViewById(R.id.tv_tu_item_content);
            imageView = (ImageView) itemView.findViewById(R.id.iv_tu_item_icon);
            imageView2 = (ImageView) itemView.findViewById(R.id.iv_tu_item_image);
        }
    }
}
